﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MO.ModelingClient
{
    public class DataConfig
    {
        public string TableName;
        public List<ItemData> FieldList = new List<ItemData>();
        public string Parent;
        public string TitleField;
        public string KeyField;
        public string OrderField;
        public Dictionary<string, object> KeyDict = new Dictionary<string, object>();
        public TableControl TableControl;
        public void AddItem(ItemData item)
        {
            AddItem(item, false);
        }
        public void AddItem(ItemData item, bool spell)
        {
            item.ParentConfig = this;
            FieldList.Add(item);
            if (spell)
            {
                item.ControlValueChanged = delegate(object value)
                    {
                        if (this.TableControl.IsAdd)
                        {
                            var keyItem = GetItemData(KeyField);
                            keyItem.ItemControl.SetValue(keyItem.Default + Spell.GetChineseSpell(value as string));
                        }
                    };
            }
        }
        public ItemData GetItemData(string code)
        {
            foreach (ItemData item in FieldList)
            {
                if (item.Code == code)
                    return item;
            }
            return null;
        }
    }
    public class ItemData
    {
        public DataConfig ParentConfig;
        public string Code;
        public string Name;
        public string Type = DataType.String;
        public bool IsDd = true;
        public string Description;
        public object Default;
        public Dictionary<string, string> Enums;
        public DataConfig ChildConfig;
        public ItemBaseControl ItemControl;
        /// <summary>
        /// 控件值变化时
        /// </summary>
        public Action<object> ControlValueChanged;
        /// <summary>
        /// 文本框在新窗口编辑
        /// </summary>
        public Func<BaseTextEditForm> GetBaseTextEditForm;
        /// <summary>
        /// 控件创建时
        /// </summary>
        public Action<ItemBaseControl> CreateControlDelegate;
        public ItemData(string code, string name)
        {
            this.Code = code;
            this.Name = name;
        }
        public ItemBaseControl CreateControl()
        {
            ItemBaseControl control = null;
            switch (this.Type)
            {
                case DataType.String: control = new ItemTextControl(this.Name, this.Default as string, GetBaseTextEditForm);
                    break;
                case DataType.Number: control = new ItemNumberControl(this.Name);
                    break;
                case DataType.Time: control = new ItemTimeControl(this.Name);
                    break;
                case DataType.Bool: control = new ItemCheckboxControl(this.Name, this.Description);
                    break;
                case DataType.Enum: control = new ItemEnumControl(this.Name, this.Enums);
                    break;
                case DataType.Field: control = new ItemFieldControl(this.Name);
                    break;
                case DataType.ChildTable: control = new ItemChildTableControl(this.Name, this.ChildConfig, ParentConfig.KeyField, ParentConfig.TableControl.GetKey());
                    break;
            }
            control.Tag = this;
            if (ControlValueChanged != null)
                control.ValueChanged = ControlValueChanged;
            ItemControl = control;
            if (CreateControlDelegate != null)
                CreateControlDelegate(control);
            return control;
        }
    }
    public class DataType
    {
        public const string String = "string";
        public const string Enum = "enum";
        public const string Number = "number";
        public const string Bool = "bool";
        public const string Field = "field";
        public const string ChildTable = "child_table";
        public const string Time = "time";
    }

}
